package com.smalls.newvideotwo.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Created by thear on 2014/12/31.
 * 描述：时间格式化工具类
 */
public class TimeFormatUtils {

    public static final String YYYY_MM_dd = "yyyy-MM-dd";
    public static final String MM_dd_HH_mm = "MM/dd HH:mm";
    public static final String HH_mm_ss = "HH:mm:ss";
    public static final String mm_ss = "mm:ss";

    /**
     * 时间转换
     *
     * @param time   单位ms
     * @param format
     */
    public static String convertTime(long time, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.CHINA);
        return formatter.format(new Date(time));
    }


    /**
     * 转成HH:mm:ss 24小时格式
     * 少于1个小时：mm:ss
     */
    public static String parseToHH_mm_ss(long time) {
        int hour = (int) (time / 60 / 60 / 1000);// 小时

        if (hour > 0) {
            return convertTime(time, HH_mm_ss);
        }
        return convertTime(time, mm_ss);
    }


    /**
     * 大于60分钟的时间间隔，格式: 01:30:09
     *
     * @param intervalTime ms
     */
    public static String getIntervalTime_parseToHH_mm_ss(long intervalTime) {
        StringBuffer sb = new StringBuffer();
        int hour = (int) (intervalTime / 1000 / 60 / 60 % 60);//时
        int minute = (int) (intervalTime / 1000 / 60 % 60);//分
        int secend = (int) (intervalTime / 1000 % 60);//秒


        sb.append(String.format(Locale.CHINA, "%02d:%02d:%02d", hour, minute, secend));

        return sb.toString();
    }


    /**
     * 将时间戳转为代表"距现在多久之前"的的友好字符串
     *
     * @param timeInMillis ms时间戳，如果服务器是unix时间戳，需*1000
     */
    public static String getFriendlyTime(long timeInMillis, String end) {
        StringBuffer sb = new StringBuffer();

        long time = System.currentTimeMillis() - timeInMillis;
        long mill = time / 1000;//秒前
        long minute = time / 60 / 1000;// 分钟前
        long hour = time / 60 / 60 / 1000;// 小时
        long day = time / 24 / 60 / 60 / 1000;// 天前

        int current_year = Calendar.getInstance().get(Calendar.YEAR);
        Calendar canlendar = Calendar.getInstance();
        canlendar.setTimeInMillis(timeInMillis);
        int interval_year = canlendar.get(Calendar.YEAR);
//	    Log.d(TAG,"interval:"+time+",mill:"+mill+",minute:"+minute+",hour:"+hour+"day:"+day);
        if (interval_year != current_year) {
            sb.append(convertTime(timeInMillis, YYYY_MM_dd));
        } else if (day >= 4) {
            sb.append(convertTime(timeInMillis, MM_dd_HH_mm));
        } else if (day >= 1 && day < 4) {
            sb.append(day + "天前");
        } else if (hour >= 1 && hour < 24) {
            sb.append(hour + "小时前");
        } else if (minute >= 1 && minute < 60) {
            sb.append(minute + "分钟前");
        } else if (mill >= 20 && mill < 60) {
            sb.append(mill + "秒前");
        } else {
            sb.append("刚刚");
        }
        sb.append(end);
        return sb.toString();
    }

    /**
     * 1年前：yyyy_mm_dd
     * 3天前：mm_dd
     * 当天：hh:mm:ss/mm:ss
     */
    public static String getFriendlyTime(long timeMills) {
        StringBuffer sb = new StringBuffer();

        Calendar canlendar = Calendar.getInstance();
        int current_year = canlendar.get(Calendar.YEAR);
        canlendar.setTimeInMillis(timeMills);
        int interval_year = canlendar.get(Calendar.YEAR);

        if (interval_year != current_year) {//1年前
            sb.append(convertTime(timeMills, YYYY_MM_dd));
        } else {

            if (timeMills < getTodayFisrtTime()) {//当天0点前
                sb.append(convertTime(timeMills, MM_dd_HH_mm));
            } else {//当天
                sb.append(parseToHH_mm_ss(timeMills));
            }
        }

        return sb.toString();
    }


    /**
     * 获取当天零点的时间戳
     */
    public static long getTodayFisrtTime() {

        Calendar canlendar = Calendar.getInstance();
        canlendar.set(Calendar.HOUR_OF_DAY, 0);
        canlendar.set(Calendar.SECOND, 0);
        canlendar.set(Calendar.MINUTE, 0);
        canlendar.set(Calendar.MILLISECOND, 1);
        return canlendar.getTimeInMillis();
    }

    /**
     * 将时间戳转距现在的倒计时，精确到秒
     *
     * @param end_time 最终截止的时间戳
     */
    public static String getCountZero(long end_time) {
        long intervaltime = end_time - System.currentTimeMillis();
        return getIntervalTime_parseToHH_mm_ss(intervaltime);
    }

    public static Date getFirstTimeData(Date today) {
        Date newDate = new Date(today.getYear(),today.getMonth(),today.getDate(),0,0,0);
        return newDate;
    }

    public static Date getNewData(Date today, int day) {
        Date newDate = new Date(today.getYear(),today.getMonth(),today.getDate(),23,59,59);
        return newDate;
    }

    public static Date getNewData(int day) {
        Date today = new Date();
        Date newDate = new Date(today.getYear(),today.getMonth(),today.getDate()+day,0,0,0);
        return newDate;
    }

}
